﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XPMVC.Database;

namespace XPMVC.Controllers
{
    public class AuthController : Controller
    {
        public Member CheckLogin(string username, string password)
        {
            using (xpEntities db = new xpEntities())
            {
                return db.Member.FirstOrDefault(x => x.username.Equals(username) && x.passwordHash.Equals(password));
            }
        }

        public Tuple<Member, string> Register(Member m)
        {
            string result = "";

            using (xpEntities db = new xpEntities())
            {
                if (db.Member.FirstOrDefault(x => x.username.Equals(m.username)) != null)
                {
                    m = null;
                    result = "Member with this username already exists";
                }
                else
                {
                    db.Member.Add(m);
                    result = db.SaveChanges() > 0 ? "Your account was successfully created" : "There was some problem";
                }
            }

            return new Tuple<Member, string>(m, result);
        }
    }
}